package com.example.payment;

import java.math.BigDecimal;

/**
 * 支付处理器 - 包含大量条件逻辑，适合策略模式重构
 */
public class PaymentProcessor {
    
    /**
     * 处理支付 - 包含复杂的条件逻辑（适合策略模式重构）
     */
    public PaymentResult processPayment(String paymentMethod, BigDecimal amount, String accountInfo) {
        PaymentResult result = new PaymentResult();
        
        if ("CREDIT_CARD".equals(paymentMethod)) {
            // 信用卡支付逻辑
            System.out.println("处理信用卡支付");
            
            if (amount.compareTo(new BigDecimal("10000")) > 0) {
                result.setSuccess(false);
                result.setErrorMessage("信用卡单笔支付金额不能超过10000元");
                return result;
            }
            
            if (accountInfo == null || accountInfo.length() < 16) {
                result.setSuccess(false);
                result.setErrorMessage("信用卡号格式错误");
                return result;
            }
            
            // 验证信用卡
            if (!validateCreditCard(accountInfo)) {
                result.setSuccess(false);
                result.setErrorMessage("信用卡验证失败");
                return result;
            }
            
            // 扣款
            boolean chargeSuccess = chargeCreditCard(accountInfo, amount);
            if (chargeSuccess) {
                result.setSuccess(true);
                result.setTransactionId("CC_" + System.currentTimeMillis());
                result.setFee(amount.multiply(new BigDecimal("0.03"))); // 3%手续费
            } else {
                result.setSuccess(false);
                result.setErrorMessage("信用卡扣款失败");
            }
            
        } else if ("PAYPAL".equals(paymentMethod)) {
            // PayPal支付逻辑
            System.out.println("处理PayPal支付");
            
            if (amount.compareTo(new BigDecimal("5000")) > 0) {
                result.setSuccess(false);
                result.setErrorMessage("PayPal单笔支付金额不能超过5000元");
                return result;
            }
            
            if (accountInfo == null || !accountInfo.contains("@")) {
                result.setSuccess(false);
                result.setErrorMessage("PayPal账户格式错误");
                return result;
            }
            
            // 验证PayPal账户
            if (!validatePayPalAccount(accountInfo)) {
                result.setSuccess(false);
                result.setErrorMessage("PayPal账户验证失败");
                return result;
            }
            
            // 扣款
            boolean chargeSuccess = chargePayPal(accountInfo, amount);
            if (chargeSuccess) {
                result.setSuccess(true);
                result.setTransactionId("PP_" + System.currentTimeMillis());
                result.setFee(amount.multiply(new BigDecimal("0.025"))); // 2.5%手续费
            } else {
                result.setSuccess(false);
                result.setErrorMessage("PayPal扣款失败");
            }
            
        } else if ("BANK_TRANSFER".equals(paymentMethod)) {
            // 银行转账逻辑
            System.out.println("处理银行转账");
            
            if (amount.compareTo(new BigDecimal("50000")) > 0) {
                result.setSuccess(false);
                result.setErrorMessage("银行转账单笔金额不能超过50000元");
                return result;
            }
            
            if (accountInfo == null || accountInfo.length() < 10) {
                result.setSuccess(false);
                result.setErrorMessage("银行账号格式错误");
                return result;
            }
            
            // 验证银行账户
            if (!validateBankAccount(accountInfo)) {
                result.setSuccess(false);
                result.setErrorMessage("银行账户验证失败");
                return result;
            }
            
            // 转账
            boolean transferSuccess = processBankTransfer(accountInfo, amount);
            if (transferSuccess) {
                result.setSuccess(true);
                result.setTransactionId("BT_" + System.currentTimeMillis());
                result.setFee(new BigDecimal("5.00")); // 固定5元手续费
            } else {
                result.setSuccess(false);
                result.setErrorMessage("银行转账失败");
            }
            
        } else if ("ALIPAY".equals(paymentMethod)) {
            // 支付宝支付逻辑
            System.out.println("处理支付宝支付");
            
            if (amount.compareTo(new BigDecimal("20000")) > 0) {
                result.setSuccess(false);
                result.setErrorMessage("支付宝单笔支付金额不能超过20000元");
                return result;
            }
            
            if (accountInfo == null || (!accountInfo.contains("@") && !accountInfo.matches("\\d{11}"))) {
                result.setSuccess(false);
                result.setErrorMessage("支付宝账户格式错误");
                return result;
            }
            
            // 验证支付宝账户
            if (!validateAlipayAccount(accountInfo)) {
                result.setSuccess(false);
                result.setErrorMessage("支付宝账户验证失败");
                return result;
            }
            
            // 扣款
            boolean chargeSuccess = chargeAlipay(accountInfo, amount);
            if (chargeSuccess) {
                result.setSuccess(true);
                result.setTransactionId("AP_" + System.currentTimeMillis());
                result.setFee(amount.multiply(new BigDecimal("0.02"))); // 2%手续费
            } else {
                result.setSuccess(false);
                result.setErrorMessage("支付宝扣款失败");
            }
            
        } else if ("WECHAT_PAY".equals(paymentMethod)) {
            // 微信支付逻辑
            System.out.println("处理微信支付");
            
            if (amount.compareTo(new BigDecimal("20000")) > 0) {
                result.setSuccess(false);
                result.setErrorMessage("微信支付单笔金额不能超过20000元");
                return result;
            }
            
            if (accountInfo == null || !accountInfo.matches("\\d{11}")) {
                result.setSuccess(false);
                result.setErrorMessage("微信账户格式错误");
                return result;
            }
            
            // 验证微信账户
            if (!validateWechatAccount(accountInfo)) {
                result.setSuccess(false);
                result.setErrorMessage("微信账户验证失败");
                return result;
            }
            
            // 扣款
            boolean chargeSuccess = chargeWechatPay(accountInfo, amount);
            if (chargeSuccess) {
                result.setSuccess(true);
                result.setTransactionId("WX_" + System.currentTimeMillis());
                result.setFee(amount.multiply(new BigDecimal("0.02"))); // 2%手续费
            } else {
                result.setSuccess(false);
                result.setErrorMessage("微信支付扣款失败");
            }
            
        } else {
            result.setSuccess(false);
            result.setErrorMessage("不支持的支付方式: " + paymentMethod);
        }
        
        return result;
    }
    
    // 模拟的验证和扣款方法
    private boolean validateCreditCard(String cardNumber) {
        return cardNumber.length() == 16;
    }
    
    private boolean chargeCreditCard(String cardNumber, BigDecimal amount) {
        return Math.random() > 0.1; // 90%成功率
    }
    
    private boolean validatePayPalAccount(String email) {
        return email.contains("@") && email.contains(".");
    }
    
    private boolean chargePayPal(String email, BigDecimal amount) {
        return Math.random() > 0.05; // 95%成功率
    }
    
    private boolean validateBankAccount(String accountNumber) {
        return accountNumber.length() >= 10;
    }
    
    private boolean processBankTransfer(String accountNumber, BigDecimal amount) {
        return Math.random() > 0.02; // 98%成功率
    }
    
    private boolean validateAlipayAccount(String account) {
        return account.contains("@") || account.matches("\\d{11}");
    }
    
    private boolean chargeAlipay(String account, BigDecimal amount) {
        return Math.random() > 0.03; // 97%成功率
    }
    
    private boolean validateWechatAccount(String phoneNumber) {
        return phoneNumber.matches("\\d{11}");
    }
    
    private boolean chargeWechatPay(String phoneNumber, BigDecimal amount) {
        return Math.random() > 0.03; // 97%成功率
    }
    
    public static class PaymentResult {
        private boolean success;
        private String transactionId;
        private String errorMessage;
        private BigDecimal fee;
        
        public boolean isSuccess() { return success; }
        public void setSuccess(boolean success) { this.success = success; }
        
        public String getTransactionId() { return transactionId; }
        public void setTransactionId(String transactionId) { this.transactionId = transactionId; }
        
        public String getErrorMessage() { return errorMessage; }
        public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; }
        
        public BigDecimal getFee() { return fee; }
        public void setFee(BigDecimal fee) { this.fee = fee; }
    }
}